﻿using System;
using System.Web.Mvc;
using System.Web.Routing;
using Zoo.Infrastructure.Data.Core;
using Zoo.Infrastructure.IoC;
using Zoo.Web.Framework.Mvc;
using Zoo.Web.Framework.Mvc.Routes;

namespace Zoo.Cat.Web {
    // 注意: 有关启用 IIS6 或 IIS7 经典模式的说明，
    // 请访问 http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters) {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes) {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            //routes.MapRoute(
            //    "Default", // 路由名称
            //    "{controller}/{action}/{id}", // 带有参数的 URL
            //    new { controller = "Home", action = "Index", id = UrlParameter.Optional } // 参数默认值
            //);

            //register custom routes (plugins, etc)
            var routePublisher = EngineContext.Current.Resolve<IRoutePublisher>();
            routePublisher.RegisterRoutes(routes);

            routes.MapRoute(
               "Default", // Route name
               "{controller}/{action}/{id}", // URL with parameters
               new { controller = "Jobseeker", action = "Login", id = UrlParameter.Optional },
               // new { controller = "Home", action = "Index", id = UrlParameter.Optional },
               new[] { "Zoo.Cat.Web.Controllers" }
           );

          

        }

        protected void Application_Start() {
           
            //initialize engine context
            EngineContext.Initialize(false);

           bool databaseInstalled = DataSettingsHelper.DatabaseIsInstalled();

            //set dependency resolver
            var dependencyResolver = new ZooDependencyResolver();
            DependencyResolver.SetResolver(dependencyResolver);

            //model binders
            ModelBinders.Binders.Add(typeof(BaseZooModel), new ZooModelBinder());

            //if (databaseInstalled)
            //{
            //    //remove all view engines
            //    ViewEngines.Engines.Clear();
            //    //except the themeable razor view engine we use
            //    ViewEngines.Engines.Add(new ThemeableRazorViewEngine());
            //}

            //Add some functionality on top of the default ModelMetadataProvider
            //ModelMetadataProviders.Current = new ZooMetadataProvider();

            //Registering some regular mvc stuf
            AreaRegistration.RegisterAllAreas();
            //if (databaseInstalled) {
            //    GlobalFilters.Filters.Add(new ProfilingActionFilter());
            //}
            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);

            //DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;

           // ModelValidatorProviders.Providers.Add(new FluentValidationModelValidatorProvider(new NopValidatorFactory()));

            ////register virtual path provider for embedded views
            //var embeddedViewResolver = EngineContext.Current.Resolve<IEmbeddedViewResolver>();
            //var embeddedProvider = new EmbeddedViewVirtualPathProvider(embeddedViewResolver.GetEmbeddedViews());
            //HostingEnvironment.RegisterVirtualPathProvider(embeddedProvider);

            ////mobile device support
            //if (databaseInstalled) {
            //    if (false) {
            //        //Enable the mobile detection provider (if enabled)
            //        HttpCapabilitiesBase.BrowserCapabilitiesProvider = new FiftyOne.Foundation.Mobile.Detection.MobileCapabilitiesProvider();
            //    }
            //    else {
            //        //set BrowserCapabilitiesProvider to null because 51Degrees assembly always sets it to MobileCapabilitiesProvider
            //        //it'll allow us to use default browserCaps.config file
            //        HttpCapabilitiesBase.BrowserCapabilitiesProvider = null;
            //    }
            //}

            ////start scheduled tasks
            //if (databaseInstalled) {
            //    TaskManager.Instance.Initialize();
            //    TaskManager.Instance.Start();
            //}

        }
        protected void Application_BeginRequest(object sender, EventArgs e) {
        }

        protected void Application_EndRequest(object sender, EventArgs e) {
        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e) {
        }

        protected void Application_Error(Object sender, EventArgs e) {
            LogException(Server.GetLastError());
        }

        protected void LogException(Exception exc) {
        }


    }
}